package com.ruijie.acl.utils;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.ruijie.acl.AccessGroup;
import com.ruijie.acl.Ace;
import com.ruijie.acl.AclDevice;
import com.ruijie.acl.AclInterface;
import com.ruijie.acl.Template;
import com.ruijie.acl.TimeInfo;
import com.ruijie.acl.TimeRange;
import com.ruijie.acl.task.model.AccessGroupInfo;
import com.ruijie.acl.task.model.AceInfo;
import com.ruijie.acl.task.model.AclDeviceInfo;
import com.ruijie.acl.task.model.AclInterfaceInfo;
import com.ruijie.acl.task.model.TimeRangeInfo;
import com.ruijie.acl.type.EthernetProtocolTypeEnum;
import com.ruijie.acl.type.EthernetTypeEnum;
import com.ruijie.emp.task.util.TaskUtil;
import com.ruijie.foundation.utils.StringUtils;

public class AclEntityUtil {
	
	/**
	 * 把从设备中取得的ACE信息转换成ACE的Entity
	 * 
	 * @param AceInfo
	 *            P信息
	 * @return ACE的Entity 
	 */
	public static Ace aceInfo2Ace(AceInfo aceInfo){
		Ace ace = new Ace(); 
		//ace.setAceNum(Integer.parseInt(aceInfo.getAceNum()));
		ace.setActionType(aceInfo.getAceAction());
		ace.setDesAddr(aceInfo.getDesAddr());
		if(aceInfo.getDesMacAddr()!=null){
			ace.setDesMacAddr(TaskUtil.transMac(aceInfo.getDesMacAddr()));
		}
		ace.setDesMacMatchType(aceInfo.getDesMacMatchType());
		ace.setDesMatchType(aceInfo.getDesMatchType());
		ace.setDesWildcard(aceInfo.getDesWildcard());
		
		if(!StringUtils.isNullOrEmpty(aceInfo.getProtocolType())){
			EthernetTypeEnum ethernetTypeEnum=EthernetTypeEnum.getEnum(aceInfo.getProtocolType());
			EthernetProtocolTypeEnum ethernetProtocolTypeEnum=EthernetProtocolTypeEnum.getEnum(aceInfo.getProtocolType());
			if(ethernetProtocolTypeEnum!=null&&ethernetTypeEnum==null){
				//扩展、专家的协议
				ace.setProtocolType(ethernetProtocolTypeEnum.toString());
			}
			if(ethernetTypeEnum!=null&&ethernetProtocolTypeEnum!=null){
				//不能确定是以太网还是扩展、专家的协议
				ace.setProtocolType(ethernetProtocolTypeEnum.toString());
				ace.setEthernetType(ethernetTypeEnum.toString());
			}
	 	}
		ace.setSourceAddr(aceInfo.getSourceAddr());
		if(aceInfo.getSourceMacAddr()!=null){
			ace.setSourceMacAddr(TaskUtil.transMac(aceInfo.getSourceMacAddr()));
		}
		ace.setSourceMacMatchType(aceInfo.getSourceMacMatchType());
		ace.setSourceMatchType(aceInfo.getSourceMatchType());
		ace.setSourceWildcard(aceInfo.getSourceWildcard());
		ace.setVlanNum(aceInfo.getVlanNum());
		ace.setTimeRangeName(aceInfo.getTimeRangeName());
		
		if (aceInfo.getSourcePort() != null){
			ace.setSourcePort(aceInfo.getSourcePort().toString());
		}
		if (aceInfo.getDesPort() != null){
			ace.setDesPort(aceInfo.getDesPort().toString());
		}
		return ace;
		
	}
	
	/**
	 * 把从设备中取得的AccessGroup信息转换成AccessGroup的Entity
	 * @param entity
	 * @return
	 */
	public static AccessGroup accessGroupInfo2AccessGroup(AccessGroupInfo entity) {
		AccessGroup group = new AccessGroup();
		group.setName(entity.getName());
		group.setAclType(entity.getAclType());
		group.setAces(aceInfoList2AceSet(entity.getAceInfoList()));
		return group;
	}
	
		
	/**
	 * 把从设备中取得的AccessGroup信息转换成Template的Entity
	 * @param entity
	 * @return
	 */
	public static Template accessGroupInfo2Template(AccessGroupInfo entity){
		Template template = new Template();
		template.setName(entity.getName());
		template.setAccessGroupType(entity.getAclType());
		template.setAces(aceInfoList2AceSet(entity.getAceInfoList()));
		return template;
	}
	
	/**
	 * 把AccessGroup信息转换成Template的Entity
	 * @param entity
	 * @return
	 */
	public static Template accessGroup2Template(AccessGroup entity){
		Template template = new Template();
		template.setName(entity.getName());
		template.setAccessGroupType(entity.getAclType());
		return template;
	}
	
	/**
	 * 把从设备中取得的AclDevice信息转换成AclDevice的Entity
	 * @param entity
	 * @return
	 */
	public static AclDevice aclDeviceInfo2AclDevice(AclDeviceInfo entity) {
		AclDevice aclDevice = new AclDevice();
		aclDevice.setAccessGroups(accessGroupInfoList2AccessGroupSet(entity.getAccessGroupInfoList()));
		aclDevice.setTimeRanges(timeRangeInfoList2TimeRangeSet(entity.getTimeRangeInfoList()));
		
		return aclDevice;
	}
	
	/**
	 * 把从设备中取得的AclInterface信息转换成AclInterface的Entity
	 * @param entity
	 * @return
	 */
	public static AclInterface aclInterfaceInfo2AclInterface(AclInterfaceInfo entity) {
		AclInterface aclInterface = new AclInterface();
		aclInterface.getDeviceInterface().setName(entity.getIfName());
		aclInterface.getOutAccessGroup().setName(entity.getInAccessGroupName());
		aclInterface.getOutAccessGroup().setName(entity.getOutAccessGroupName());
		return aclInterface;
	}
	
	/**
	 * 把从设备中取得的TimeInfo信息转换成TimeInfo的Entity
	 * @param entity
	 * @return
	 */
	public static TimeInfo timeInfo2TimeInfo(com.ruijie.acl.task.model.TimeInfo entity) {
		TimeInfo timeInfo = new TimeInfo();
		timeInfo.setStarttime(entity.getStartTime());
		timeInfo.setEndtime(entity.getEndTime());
		timeInfo.setPeriodic(entity.getPeriodic().toLowerCase());
		return timeInfo;
	}
	
	/**
	 * 把从设备中取得的TimeRange信息转换成TimeRange的Entity
	 * @param entity
	 * @return
	 */
	public static TimeRange timeRangeInfo2TimeRange(TimeRangeInfo entity) {
		TimeRange timeRange = new TimeRange();
		timeRange.setName(entity.getName());
		timeRange.setIsChanged(false);
		timeRange.setStartDate(entity.getAbsoluteStartDate());
		timeRange.setEndDate(entity.getAbsoluteEndDate());
		//timeRange.setTimeInfos(timeInfoList2TimeInfoSet(entity.getTimeInfoList()));
		timeRange.setTimeInfoList(timeInfoList2TimeInfoList(entity.getTimeInfoList()));
		return timeRange;
	}
	
	/**
	 * 把List<com.ruijie.acl.task.model.TimeInfo>信息转换成Set<TimeInfo>的Entity
	 * @param entity
	 * @return
	 */
	public static Set<TimeInfo> timeInfoList2TimeInfoSet(List<com.ruijie.acl.task.model.TimeInfo> entity) {
		Set<TimeInfo> entitySet = new HashSet<TimeInfo>();
		if (entity != null && entity.size() > 0){
			for(com.ruijie.acl.task.model.TimeInfo info : entity) {
				entitySet.add(timeInfo2TimeInfo(info));
			}
		}
		return entitySet;
	}
	/**
	 * 把List<com.ruijie.acl.task.model.TimeInfo>信息转换成List<TimeInfo>的Entity
	 * @param entity
	 * @return
	 */
	public static List<TimeInfo> timeInfoList2TimeInfoList(List<com.ruijie.acl.task.model.TimeInfo> entity) {
		List<TimeInfo> entityList = new ArrayList<TimeInfo>();
		if (entity != null && entity.size() > 0){
			for(com.ruijie.acl.task.model.TimeInfo info : entity) {
				entityList.add(timeInfo2TimeInfo(info));
			}
		}
		return entityList;
	}
	/**
	 * 把List<AccessGroupInfo>信息转换成Set<AccessGroup>的Entity
	 * @param entity
	 * @return
	 */
	public static Set<AccessGroup> accessGroupInfoList2AccessGroupSet(List<AccessGroupInfo> entity) {
		Set<AccessGroup> entitySet = new HashSet<AccessGroup>();
		if (entity != null && entity.size() > 0){
			for(AccessGroupInfo info : entity) {
				entitySet.add(accessGroupInfo2AccessGroup(info));
			}
		}
		return entitySet;
	}
	
	/**
	 * 把List<AccessGroupInfo>信息转换成List<AccessGroup>的Entity
	 * @param entity
	 * @return
	 */
	public static List<AccessGroup> accessGroupInfoList2AccessGroupList(List<AccessGroupInfo> entity) {
		List<AccessGroup> entityList = new ArrayList<AccessGroup>();
		if (entity != null && entity.size() > 0){
			for(AccessGroupInfo info : entity) {
				entityList.add(accessGroupInfo2AccessGroup(info));
			}
		}
		return entityList;
	}
	
	/**
	 * 把List<TimeRangeInfo>信息转换成Set<TimeRange>的Entity
	 * @param entity
	 * @return
	 */
	public static Set<TimeRange> timeRangeInfoList2TimeRangeSet(List<TimeRangeInfo> entity) {
		Set<TimeRange> entitySet = new HashSet<TimeRange>();
		if (entity != null && entity.size() > 0){
			for(TimeRangeInfo info : entity) {
				entitySet.add(timeRangeInfo2TimeRange(info));
			}
		}
		return entitySet;
	}
	
	/**
	 * 把List<AceInfo>信息转换成List<Ace>的Entity
	 * @param entity
	 * @return
	 */
	public static List<Ace> aceInfoList2AceSet(List<AceInfo> entity) {
		List<Ace> entitySet = new ArrayList<Ace>();
		if (entity != null && entity.size() > 0){
			for(AceInfo info : entity) {
				entitySet.add(aceInfo2Ace(info));
			}
		}
		return entitySet;
	}
	
	/**
	 * 把List<TimeRangeInfo>信息转换成Set<TimeRange>的Entity
	 * @param entity
	 * @return
	 */
	public static List<TimeRange> timeRangeInfoList2TimeRangeList(List<TimeRangeInfo> entityList) {
		List<TimeRange> list = new ArrayList<TimeRange>();
		if (entityList != null && entityList.size() > 0){
			for(TimeRangeInfo info : entityList) {
				list.add(timeRangeInfo2TimeRange(info));
			}
		}
		return list;
	}
}
